package com.example.SMS;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.stage.Stage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginController {
    @FXML
    private Hyperlink login_register; // 注册链接
    private Button login_login;// 登陆按钮
    private Stage stage; // 当前窗口
    private Scene registerScene; // 注册界面的 Scene
    private Scene managerScene; // 管理员界面的 Scene
    private Scene oncampusScene; // 校内人员界面的 Scene
    private Scene offcampusScene; // 校外人员界面的 Scene

    // 设置 Stage 和 Scene（由主程序调用）
    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public void setRegisterScene(Scene registerScene) {
        this.registerScene = registerScene;
    }

    public void setManagerScene(Scene managerScene) {
        this.managerScene = managerScene;
    }

    public void setOncampusScene(Scene oncampusScene) {
        this.oncampusScene = oncampusScene;
    }

    public void setOffcampusScene(Scene offcampusScene) {
        this.offcampusScene = offcampusScene;
    }

    @FXML
    private javafx.scene.control.TextField login_tel; // 电话输入框
    @FXML
    private javafx.scene.control.TextField login_pwd; // 密码输入框

    //
    private void showAlert(Alert.AlertType type, String title, String message) {
        Alert alert = new Alert(type);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

    // 清空所有输入框的方法
    private void clearInputFields() {
        login_tel.clear();
        login_pwd.clear();
    }

    // Hyperlink 点击事件处理
    @FXML
    private void handleRegisterLinkClick(ActionEvent event) {
        if (stage != null && registerScene != null) {
            stage.setScene(registerScene); // 切换到注册界面
        } else {
            System.err.println("Stage 或 RegisterScene 未初始化！");
        }
    }

    // Button 点击事件处理
    @FXML
    private void handleLoginButtonClick(ActionEvent event) {
        // 用户登陆
        String tel = login_tel.getText();
        String password = login_pwd.getText();
        // 检查用户名和密码是否正确
        // 在数据库中搜索
        String sql = "SELECT usertype FROM userinformation WHERE tel = ? AND pwd = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, tel);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                String userType = rs.getString("usertype");
                showAlert(Alert.AlertType.INFORMATION, "成功", "登录成功");
                clearInputFields();

                // 根据用户类型切换到相应的场景
                switch (userType) {
                    case "管理员":
                        if (managerScene != null) {
                            stage.setScene(managerScene);
                        } else {
                            System.err.println("管理员场景未初始化！");
                        }
                        break;
                    case "校内人员":
                        if (oncampusScene != null) {
                            stage.setScene(oncampusScene);
                        } else {
                            System.err.println("校内人员场景未初始化！");
                        }
                        break;
                    case "校外人员":
                        if (offcampusScene != null) {
                            stage.setScene(offcampusScene);
                        } else {
                            System.err.println("校外人员场景未初始化！");
                        }
                        break;
                    default:
                        showAlert(Alert.AlertType.ERROR, "错误", "未知的用户类型：" + userType);
                }
            } else {
                showAlert(Alert.AlertType.WARNING, "警告", "用户名不存在或密码错误");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            showAlert(Alert.AlertType.ERROR, "错误", "数据库错误：" + e.getMessage());
        }
    }
}

